﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// CMobileList의 요약 설명입니다.
/// </summary>
public class CMobileList :CModelBase
{

    public ArrayList GetNewsFeedList(string start, string pagesize)
    {
        // 파라미터 & 조건 체크
        if ( start == "" || pagesize == "")
            throw (new Exception("필수 항목이 빠져있습니다."));

        CheckNumber(start);
        CheckNumber(pagesize);

        // 업무 수행
        GetConnection();

        ArrayList list = new ArrayList();

        try
        {
            string tail = " WHERE status=1 ";
            string sql = "SELECT COUNT(*) FROM NEWS " + tail;

            string totalcount = GetQueryString(sql);
            list.Add(totalcount);

            sql = "SELECT * " +
                " FROM (SELECT title,contents,newssource,imagepath,url,issuedate,ROW_NUMBER() OVER (ORDER BY issuedate DESC) AS rownum FROM NEWS " + tail + ") AS NEWS WHERE rownum BETWEEN " + start + "+1 AND " + start + "+" + pagesize;
            
            SqlDataReader dr = GetQueryResult(sql);
            ConvertDrToArrayList(dr, list);
            dr.Close();
        }
        finally
        {
            ReleaseConnection();
        }

        return list;
    }

    public ArrayList GetMemberList(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string cpage = CheckNull(param["page"]);
        string clistcount = CheckNull(param["listcount"]);
        string is_send = CheckNull(param["is_send"]);
        // 업무 수행
        GetConnection();

        ArrayList list = new ArrayList();

        int page = Convert.ToInt32(cpage);
        int listcount = Convert.ToInt32(clistcount);

        try
        {
            string tail = " WHERE 1=1 ";

            if (is_send != "")
                tail += "AND IsSendMail = " + is_send;

            string sql = "SELECT COUNT(*) FROM MEMBER " + tail;

            string totalcount = GetQueryString(sql);
            list.Add(totalcount);
            
            sql = "SELECT * " +
                " FROM (SELECT MemberIdx, CompanyName, Name, Email, WriteDate, IsSendMail,ROW_NUMBER() OVER (ORDER BY writedate DESC) AS rownum FROM MEMBER " + tail + ") AS MEMBER WHERE rownum BETWEEN " + ((page - 1) * listcount + 1) + " AND " + (page * listcount);

            SqlDataReader dr = GetQueryResult(sql);
            ConvertDrToArrayList(dr, list);
            dr.Close();
        }
        finally
        {
            ReleaseConnection();
        }

        return list;
    }

    public ArrayList GetFeedList(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string searchselect = CheckNull(param["searchselect"]);
        string searchvalue = CheckNull(param["searchvalue"]);
        string cpage = CheckNull(param["page"]);
        string clistcount = CheckNull(param["listcount"]);
        string status = CheckNull(param["status"]);

        // 업무 수행
        GetConnection();

        ArrayList list = new ArrayList();

        int page = Convert.ToInt32(cpage);
        int listcount = Convert.ToInt32(clistcount);

        try
        {
            string tail = " WHERE 1=1 ";

            if (searchselect == "")
            {
                if (searchvalue != "")
                    tail += " AND title LIKE '%" + MRF(searchvalue) + "%' OR newssource LIKE '%" + MRF(searchvalue) + "%' ";
            }
            else
            {
                if (searchvalue != "")
                    tail += " AND " + searchselect + " LIKE '%" + MRF(searchvalue) + "%' ";
            }

            if (status != "")
                tail += "AND status = " + status;

            string sql = "SELECT COUNT(*) FROM NEWS " + tail;

            string totalcount = GetQueryString(sql);
            list.Add(totalcount);

            sql = "SELECT * " +
                " FROM (SELECT NewsIdx, Title, NewsSource, ImagePath, URL, Status, IssueDate, ROW_NUMBER() OVER (ORDER BY IssueDate DESC) AS rownum FROM NEWS " + tail + ") AS NEWS WHERE rownum BETWEEN " + ((page - 1) * listcount + 1) + " AND " + (page * listcount);

            SqlDataReader dr = GetQueryResult(sql);
            ConvertDrToArrayList(dr, list);
            dr.Close();
        }
        finally
        {
            ReleaseConnection();
        }

        return list;
    }


    public ArrayList GetOrganList(CUserInfo userinfo, Hashtable param)
    {
        // 파라미터 & 조건 체크
        string searchselect = CheckNull(param["searchselect"]);
        string searchvalue = CheckNull(param["searchvalue"]);
        string cpage = CheckNull(param["page"]);
        string clistcount = CheckNull(param["listcount"]);

        // 업무 수행
        GetConnection();

        ArrayList list = new ArrayList();

        int page = Convert.ToInt32(cpage);
        int listcount = Convert.ToInt32(clistcount);

        try
        {
            string tail = " WHERE 1=1 ";

            if (searchselect == "")
            {
                if (searchvalue != "")
                    tail += " AND organname LIKE '%" + MRF(searchvalue) + "%' OR organcode LIKE '%" + MRF(searchvalue) + "%' OR url LIKE '%" + MRF(searchvalue) + "%' ";
            }
            else
            {
                if (searchvalue != "")
                    tail += " AND " + searchselect + " LIKE '%" + MRF(searchvalue) + "%' ";
            }

            string sql = "SELECT COUNT(*) FROM ORGAN " + tail;
            CSiteUtility.WriteLog("A", sql);
            string totalcount = GetQueryString(sql);
            list.Add(totalcount);

            sql = "SELECT * " +
                " FROM (SELECT OrganIdx, OrganName, OrganCode, URL, WriteDate,  ROW_NUMBER() OVER (ORDER BY WriteDate DESC) AS rownum FROM ORGAN " + tail + ") AS ORGAN WHERE rownum BETWEEN " + ((page - 1) * listcount + 1) + " AND " + (page * listcount);

            SqlDataReader dr = GetQueryResult(sql);
            ConvertDrToArrayList(dr, list);
            dr.Close();
        }
        finally
        {
            ReleaseConnection();
        }

        return list;
    }
    
}
